import Format from '@/utils/format'
import {deleteRecord, cleanInitVal} from '@/services/model/project-info'
import DialogRemark from '../modules/dialog-remark.vue'
import MonitorRemark from '../modules/monitor-remark'
import MonitorReset from '../modules/monitor-reset'
import Permission from '@/utils/permission'

export default {
  props: ['monitorInfo', 'startTimer', 'endTimer', 'loading'],
  components: {DialogRemark, MonitorRemark, MonitorReset},
  data() {
    return {
      collectionTime: null,
      // 分页参数
      form: {
        page: 1,
        pageSize: 10,
        total: 0,
      },
      lastData:'',
      equipId: '',
      isAutoRecord:'',
      isShowDialog: false,
      // 是否折叠
      isToggleStatus: false,
      // 采集数据
      collectionList: [],
      // 时间选择器配置
      pickerOptions: {
        shortcuts: [{
          text: '近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '近半年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 182)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '近一年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 365)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '近两年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 365*2)
            picker.$emit('pick', [start, end])
          }
        }],
        onPick: obj => {
          this.picerMinDate = new Date(obj.minDate).getTime()
        },
        disabledDate: time => {
          // if (this.picerMinDate) {
          //   // 最多可选3个月
          //   const LIMIT = 90 * 24 * 60 * 60 * 1000
          //   const maxTime = this.picerMinDate + LIMIT
          //   const minTime = this.picerMinDate - LIMIT
          //   return time.getTime() > maxTime || time.getTime() < minTime
          // }
        }
      },
      // 列表勾选的数据
      selectionList: [],
      // 展示修改 数据说明弹窗
      isShowRemarkDialog: false
    }
  },
  computed: {
    //久感监测
    jiuGanKeJi() {
      //jcdw-admin 和 jgkj-admin 是公司账号
      return this.$store.state.userInfo.organizationId === '1435423548344758274' || this.$store.state.userInfo.organizationId === '1432987939817877505'
    },
    //贵州勘设股份
    isKsGf() {
      return this.$store.state.userInfo.organizationId === '1463109589672177666'
    },
    // table分页
    tableList() {
      return this.collectionList.slice((this.form.page - 1) * this.form.pageSize, this.form.page * this.form.pageSize)
    },
    // 时间赛选
    timerArray: {
      get() {
        return [this.startTimer, this.endTimer]
      },
      set([startTimer, endTimer]) {
        this.$emit('update:startTimer', startTimer)
        this.$emit('update:endTimer', endTimer)
      }
    },
    // true  监测单位显示
    isAuthority() {
      return this.$store.state.userInfo.unitType === 4
    }
  },
  filters: {
    // 时间格式化
    formatTimer(timer) {
      return Format(new Date(timer))
    },
    calcAngle(x){
      let angle = Math.atan(x[1]/x[0])*180/Math.PI;
      if (x[1] >= 0 && x[0] >= 0) {
        return angle.toFixed(1)
      } else if (x[1] >= 0 && x[0] < 0) {
        //第二象限
        return (angle + 180).toFixed(1);
      } else if (x[1] < 0 && x[0] < 0) {
        //第三象限
        return (angle + 180).toFixed(1);
      } else {
        //第四象限
        return (angle + 360).toFixed(1);
      }
    },
    // 时间格式化
    formatTimer2(timer) {
      if (!timer) {
        return '暂无数据接入'
      }
      return Format(new Date(timer), 'yyyy年MM月dd日')
    },
    // 时间格式化
    formatTimer3(timer) {
      if (!timer) {
        return '暂无数据接入'
      }
      return Format(new Date(timer), 'yyyy-MM-dd hh:mm:ss')
    },
  },
  methods: {
    // 判断权限
    hasPermission(permissionStr) {
      return Permission(this.$store.state.userInfo, permissionStr)
    },
    // 设置预警线
    setWarningLine(list) {
      this.chartsName.map(name => {
        this.$refs[name].setWarningLine(list)
      })
    },
    // 展示数据说明
    showRemarkDialog(row) {
      this.$refs.remark.show({...row, pointId: this.monitorInfo.id, _time: Format(new Date(row._time))})
    },
    // 展示数据说明
    showMonitorRemarkDialog() {
      this.$refs.monitorRemark.show({monitorInfo: this.monitorInfo})
    },
    // 设备重置
    showMonitorResetDialog(field,typeName) {
      let title='提示：填入值为需要的相对值'
      if (field === 'inRealAngle' && this.selectionList.length !== 1) {
        this.$message({
          type: 'warning',
          title: '提示',
          center: true,
          message: '请选择一条数据作为基准值'
        })
        return
      }
      if (field === 'realValue' && this.selectionList.length !== 1) {
        this.$message({
          type: 'warning',
          title: '提示',
          center: true,
          message: '请选择一条数据作为基准值'
        })
        return
      }
      '提示：填入值为需要的相对倾角值'
      //field 为基准值字段
      const params = {
        pointId: this.monitorInfo.id,
        recordId: this.selectionList[0] ? this.selectionList[0].id : '',
        value: this.selectionList[0] ? this.selectionList[0][field] : '',
        title:title

      }
      if(field === 'realValue'){
        params.value=0
        params.title='提示：填入值为需要的相对位移值'
      }else if(field === 'inRealAngle'){
        params.value=this.selectionList[0] ? this.selectionList[0][typeName] : '',
        params.title='提示：填入值为需要的相对倾角值'
      }
      this.$refs.monitorReset.show(params)

    },
    // 预警值重置
    showMonitorResetWaringDialog(field) {
      //field 为基准值字段
      const params = {
        warning: true,
        pointId: this.monitorInfo.id,
        value: this.selectionList[0] ? this.selectionList[0][field] : ''
      }
      this.$refs.monitorReset.show(params)
    },
    // 查看设备运行日志
    lookEquipRecord(timerArray, equipId) {
      if (!equipId) {
        this.$message.info('没有设备信息！')
        return
      }
      this.$router.push({name: 'equip-record', params: {equipId: equipId}})
    },
    // 数据刷新
    update({data, monitorName, monitorInfo}) {
      this.equipId = monitorInfo.bindEquipId
      this.collectionList = data.recordList || []
      this.form.page = 1;
      this.form.total = this.collectionList.length
      this.chartsName.map(async name => {
        this.$refs[name].updateEchart(data, monitorName, monitorInfo)
      })
    },
    // 折叠面板
    toggleStatus() {
      this.isToggleStatus = !this.isToggleStatus
      setTimeout(() => {
        this.chartsName.map(name => {
          this.$refs[name].echartResize()
        })
      }, 301)
    },
    // 当前页码改变
    handleCurrentChange(val) {
      this.form.page = val
    },
    // 分页大小改变
    handleSizeChange(val) {
      this.form.pageSize = val
      this.queryList()
    },
    handleSelectionChange(val) {
      this.selectionList = val
    },
    // 删除
    confirmDelete() {
      if (!this.selectionList.length) {
        this.$message.warning('当前未勾选数据')
      } else {
        this.$confirm(`即将删除勾选${this.selectionList.length}条数据, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.submitDelete()
        })
      }
    },
    // 确认重置操作
    confirmCleanBaseRecord() {
      if (this.selectionList.length !== 1) {
        this.$message({
          type: 'warning',
          title: '提示',
          center: true,
          message: '请选择一条数据作为基准值'
        })
      } else if (this.selectionList.length === 1) {
        this.$confirm(`即将重置设备基准值, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.cleanBaseRecord()
        })
      }
    },
    //重置基准值
    async cleanBaseRecord() {
      const params = {
        pointId: this.monitorInfo.id,
        recordId: this.selectionList[0].id
      }
      let res = await cleanInitVal(params)
      if (res.code === 200) {
        this.$notify({
          type: 'success',
          title: '提示',
          message: '已重置基准值'
        })
      }
    },
    // 删除提交操作
    async submitDelete() {
      const params = {
        pointId: this.monitorInfo.id,
        dataIdArr: this.selectionList.map(x => x.id)
      }
      await deleteRecord(params)
      this.$message.success('数据已删除')
      setTimeout(() => {
        this.$emit('update')
      }, 800);
    },
  },
}
